<template>
  <div class="shareList">
    <hmNanBar path="/home/find" title="面经分享"></hmNanBar>
    <div class="conten">
      <van-search
        class="search"
        v-model="inputValue"
        placeholder="请输入内容"
        show-action
        @search="search"
        @cancel="cancelEvent"
      >
      </van-search>
      <div v-if="showList">
        <van-list
          v-if="bol"
          v-model="loading"
          :finished="finished"
          finished-text="没有了哦~"
          @load="load"
        >
          <shareItem
            v-for="(item, index) in list"
            :key="index"
            :info="item"
          ></shareItem>
        </van-list>
      </div>
      <div v-else>
        <h5>大家都在搜</h5>
        <van-tag
          class="tag"
          color="#f7f4f5"
          text-color="#73748a"
          @click="tagClick(item)"
          v-for="(item, index) in hotList"
          :key="index"
          >{{ item }}</van-tag
        >
        <van-cell class="mm">
          <template #title>
            <div class="title">
              历史搜索
            </div>
          </template>
          <template #default>
            <div class="default" @click="clear">
              清空
            </div>
          </template>
        </van-cell>
        <van-tag
          class="tag"
          color="#f7f4f5"
          text-color="#73748a"
          @click="tagClick(item)"
          v-for="(item, index) in localist"
          :key="index"
          >{{ item }}</van-tag
        >
      </div>
    </div>
  </div>
</template>

<script>
import { articlesShare, articlesTechnicTopSearch } from '@/api/find.js'
import { setLocal, getLocal } from '@/utils/local.js'
import shareItem from './shareItem'
export default {
  name: 'shareList',
  components: {
    shareItem
  },
  data () {
    return {
      inputValue: '',
      list: [],
      loading: false,
      finished: false,
      currpage: 1,
      pageSize: 2,
      bol: true,
      showList: true,
      hotList: [],
      localist: JSON.parse(getLocal('history')) || []
    }
  },
  async created () {},
  activated () {
    window.scrollTo(0, this.$route.meta.scrollTop)
  },
  methods: {
    async load () {
      const res = await articlesShare({
        start: (this.currpage - 1) * this.pageSize,
        limit: this.pageSize,
        q: this.inputValue
      })
      if (this.inputValue.trim() !== '') {
        res.data.data.list.forEach(item => {
          const _arr = item.title.split(this.inputValue)
          item.title = _arr.join(
            `<span style="color:red">${this.inputValue}</span>`
          )
        })
      }
      //   获取数据后累加到list
      this.list.push(...res.data.data.list)
      //   每一次加载完成就让loading改变为fasle
      this.loading = false
      this.currpage++
      if (this.list.length >= res.data.data.total) {
        this.finished = true
      }
    },
    search () {
      this.list = []
      this.loading = false
      this.finished = false
      this.currpage = 1
      //   静默刷新
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
      this.showList = true
      // 解决空字符
      if (this.inputValue.trim() !== '') {
        this.localist.unshift(this.inputValue)
      }
      // 解决去重
      this.localist = [...new Set(this.localist)]
      // 取到前5数据
      this.localist.splice(5)
      // window.console.log(this.localist)
      // 数据缓存
      setLocal('history', JSON.stringify(this.localist))
    },

    async cancelEvent () {
      this.showList = false
      if (this.hotList.length === 0) {
        const res = await articlesTechnicTopSearch()
        // window.console.log(res)
        this.hotList = res.data.data
      }
    },
    clear () {
      this.localist = []
      setLocal('history', JSON.stringify(this.localist))
    },
    tagClick (str) {
      this.inputValue = str
      this.search()
    }
  }
}
</script>

<style lang="less" scoped>
.shareList {
  .conten {
    .search {
      padding: 0 @p15;
    }
    .van-cell {
      padding-left: 0;
      padding-right: 0;
    }
    .van-tag {
      margin: 10px;
      padding: 9px 12px;
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #545671;
      line-height: 16px;
      letter-spacing: 0px;
      border-radius: 5px;
    }
    .title {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Medium;
      font-weight: 500;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
    }
    h5 {
      margin-bottom: 18px;
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Medium;
      font-weight: 500;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
    }
    .mm {
      margin-top: 45px;
    }
  }
}
</style>
